import http = require("http")
import { json } from "stream/consumers";
import url = require("url")
import ConfigData from "./Module/ConfigData";

class tmp{
    public param1:number = 0;
    public param2:number    = 0;
    public str1:string = "";

}

export default class HttpServer{
     
    public m_pMySql:any = null;

    public constructor(){
        var mysql      = require('mysql');
        var pDBCfg = ConfigData.getInstance().server_json["db"];
        this.m_pMySql = mysql.createConnection(pDBCfg);
        // this.m_pMySql = mysql.createConnection({
        //     host     : pDBCfg.host,
        //     user     : pDBCfg.user,
        //     password : pDBCfg.password,
        //     database : pDBCfg.database
        // });
        //console.log(mysql);
        this.m_pMySql.connect();
        this.m_pMySql.query("select * from tba ", function (error, results, fields) {
            if (error) throw error;
            console.log(results[0].val);
            //console.log(fields);
        });

        //console.log(this.m_pMySql);
    }

    public StartServer():void{
        var server = http.createServer(this.onHttp.bind(this));
        server.listen(8080);
    }

    public onHttp(req:http.IncomingMessage,res:http.ServerResponse):void{
        var urlstr:string = "";
        if(req.url != null){
            urlstr = req.url;
        }
        console.log(req.method);
        console.log(urlstr);
        
        // this.m_pMySql.query("select * from tba ", function (error, results, fields) {
        //     if (error) throw error;
        //     console.log(results);
        //     //console.log(fields);
        //  });
        // console.log("fire onhttp");
        var p = new tmp;
        res.write(JSON.stringify(p));
        res.end();
    }
}
